﻿using System;
using System.IO;
using Cpuss.Strategies;
using NUnit.Framework;

namespace Cpuss.Tests
{

    /// <summary>
    /// Tests for Report.
    /// </summary>
    [TestFixture]
    public class ReportTest
    {

        /// <summary>
        /// Check to see that the RepeatRunner used for the report is not null.
        /// </summary>
        [Test]
        public void RepeatRunnerNotNullTest()
        {
            Report actual = new Report(new RepeatRunner(10, 2, 1, new FirstComeFirstServed(), 5), "Test", Directory.GetCurrentDirectory());

            Assert.IsNotNull(actual.RepeatRunner);
        }

        /// <summary>
        /// Check to see that the correct exception is raised when the RepeatRunner is null.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RepeatRunnerNullTest()
        {
            RepeatRunner repeatRunner = null;
            Report actual = new Report(repeatRunner, "Test", Directory.GetCurrentDirectory());
        }

        /// <summary>
        /// Check to see that the file name is correct.
        /// </summary>
        [Test]
        public void FileNameTest()
        {
            Report actual = new Report(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 3), "Test", Directory.GetCurrentDirectory());

            Assert.AreEqual("Test.html", actual.FileName);
        }

        /// <summary>
        /// Check to see that the correct exception is raised when the file name is null.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FileNameNullTest()
        {
            Report actual = new Report(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 2), null, Directory.GetCurrentDirectory());
        }

        /// <summary>
        /// Check to see that the correct exception is raised when the file name is of length 0.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void FileNameLessThanOneCharInLengthTest()
        {
            Report actual = new Report(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 3), "", Directory.GetCurrentDirectory());
        }

        /// <summary>
        /// Check to see that the output dir is correct.
        /// </summary>
        [Test]
        public void OutputDirTest()
        {
            Report actual = new Report(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 2), "test", @"C:\");

            Assert.AreEqual(@"C:\", actual.OutputDirectory);
        }

        /// <summary>
        /// Check to see that the correct exception is thrown when the output dir is null.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void OutputDirNullTest()
        {
            Report actual = new Report(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 4), "test", null);
        }

        /// <summary>
        /// Check to see that the output dir specified is at least 3 chars in length, that is X:\
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void OutputDirProvidedNotValid()
        {
            Report actual = new Report(new RepeatRunner(1, 2, 34, new FirstComeFirstServed(), 4), "test", "C:");
        }

        /// <summary>
        /// Check to see that the correct exception is thrown when the output dir provided doesn't exist.
        /// </summary>
        [Test]
        [ExpectedException(typeof(DirectoryNotFoundException))]
        public void OutputDirNotLocatedTest()
        {
            Report actual = new Report(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 2), "test", @"c:\nonexistant");
        }

        /// <summary>
        /// Check to see that the report file exists.
        /// </summary>
        [Test]
        public void CreateTest()
        {
            Guid id = Guid.NewGuid();
            Report actual = new Report(new RepeatRunner(10, 12, 8, new ShortestJobFirst(1), 5), id.ToString(), Directory.GetCurrentDirectory());

            actual.Create();

            Assert.IsTrue(File.Exists(string.Format("{0}\\{1}.html", Directory.GetCurrentDirectory(), id.ToString())));
        }

    }

}
